import { viewList } from '@/service';
import { IViewType } from '@/types';
import { Effect, Reducer } from 'umi';

export interface ViewModelState {
  viewList:IViewType  [],
}

export interface ViewModelType {
  namespace: 'view';
  state: ViewModelState;
  effects: {
    viewList: Effect;
  };
  reducers: {
    save: Reducer<ViewModelState>;
  };
}

const viewModel: ViewModelType = {
  namespace: 'view',
  state: {
    viewList: [],
  },

  effects: {
    *viewList({payload}, {call, put}){
        let result = yield viewList(payload);
      if (result.data){
        yield put({
          type: 'save',
          payload: {
            viewList: result.data[0]
          }
        })
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default viewModel;
